{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.235819Z",
     "start_time": "2024-12-30T02:56:42.698913Z"
    }
   },
   "source": [
    "# 从sklearn 模块下的线性模块下导入多元线性回归类\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.242920Z",
     "start_time": "2024-12-30T02:56:46.236842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构建真实的X和y\n",
    "X1 = 2 * np.random.rand(100, 1)\n",
    "X2 = 2 * np.random.rand(100, 1)\n",
    "X = np.c_[X1, X2]\n",
    "X"
   ],
   "id": "e05be24a0c7fe1d7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.87167719, 1.83134496],\n",
       "       [0.7394302 , 0.73986836],\n",
       "       [1.23765353, 1.55976211],\n",
       "       [1.84463519, 0.10461777],\n",
       "       [1.50201878, 0.43247731],\n",
       "       [0.11501964, 1.83972334],\n",
       "       [1.40886991, 0.8265033 ],\n",
       "       [0.11034193, 0.66074768],\n",
       "       [1.62799586, 0.92246935],\n",
       "       [1.9382521 , 1.05938041],\n",
       "       [1.0830869 , 1.90787345],\n",
       "       [1.30503789, 0.76770812],\n",
       "       [0.33185234, 1.64488565],\n",
       "       [1.26734193, 0.58526616],\n",
       "       [1.49363722, 1.36591545],\n",
       "       [1.76358754, 1.52090764],\n",
       "       [0.66876409, 0.8802123 ],\n",
       "       [0.40227347, 1.21678905],\n",
       "       [1.87682217, 1.64991814],\n",
       "       [0.3689005 , 0.21855264],\n",
       "       [1.5360265 , 1.4021032 ],\n",
       "       [0.61223317, 1.97876819],\n",
       "       [0.70433354, 0.30282305],\n",
       "       [1.84005766, 0.9739427 ],\n",
       "       [0.5975685 , 0.43139896],\n",
       "       [0.20190824, 1.79888271],\n",
       "       [0.1694105 , 1.94912321],\n",
       "       [0.39139119, 0.36720674],\n",
       "       [1.19301493, 0.40619726],\n",
       "       [0.85889683, 1.80654705],\n",
       "       [0.8314802 , 1.76648652],\n",
       "       [0.13248563, 1.16050584],\n",
       "       [1.58086292, 1.50929619],\n",
       "       [0.98743316, 0.1148007 ],\n",
       "       [0.24274994, 1.36831915],\n",
       "       [1.17508577, 0.83389211],\n",
       "       [1.31855137, 1.02801532],\n",
       "       [1.69150779, 0.36006391],\n",
       "       [1.98021885, 0.97257242],\n",
       "       [1.12529117, 1.07373094],\n",
       "       [1.5265929 , 1.28757296],\n",
       "       [0.70199235, 0.37334921],\n",
       "       [0.98599603, 1.55945058],\n",
       "       [0.8137031 , 1.18085352],\n",
       "       [0.67433872, 1.195621  ],\n",
       "       [0.43063728, 1.69443923],\n",
       "       [0.70230658, 1.71078233],\n",
       "       [0.36169791, 1.12995744],\n",
       "       [0.16064437, 0.70653606],\n",
       "       [0.31235448, 1.79817856],\n",
       "       [0.43959414, 1.61075521],\n",
       "       [1.75788014, 0.19692501],\n",
       "       [0.89328652, 0.92088977],\n",
       "       [1.68884512, 0.98572648],\n",
       "       [1.69511975, 1.01073238],\n",
       "       [0.61235739, 1.74161603],\n",
       "       [1.66149648, 0.01156745],\n",
       "       [1.48484374, 0.65008677],\n",
       "       [1.74970683, 1.45126035],\n",
       "       [0.22802551, 1.30633125],\n",
       "       [0.8760271 , 1.06241931],\n",
       "       [0.33528631, 1.2356483 ],\n",
       "       [1.81626128, 0.67161934],\n",
       "       [1.01785179, 0.30815677],\n",
       "       [1.84664594, 0.17683813],\n",
       "       [1.25350505, 1.28298119],\n",
       "       [1.23294335, 1.23456059],\n",
       "       [0.14455537, 0.81888573],\n",
       "       [1.38796428, 1.33185469],\n",
       "       [0.24972242, 1.46393689],\n",
       "       [0.85544498, 1.78788103],\n",
       "       [1.52027683, 0.80108239],\n",
       "       [1.64087805, 0.43041653],\n",
       "       [0.93771357, 0.18202584],\n",
       "       [1.28704591, 0.09639068],\n",
       "       [1.97101393, 0.4161382 ],\n",
       "       [1.84404737, 1.09773057],\n",
       "       [0.20571576, 1.93606122],\n",
       "       [1.57528725, 1.10291776],\n",
       "       [0.0064885 , 1.30148084],\n",
       "       [0.7527479 , 0.91893559],\n",
       "       [0.60549915, 0.05786361],\n",
       "       [1.42486039, 0.13994595],\n",
       "       [1.93342331, 0.84785081],\n",
       "       [1.85919291, 0.44916826],\n",
       "       [1.42141988, 0.42282664],\n",
       "       [0.05110918, 0.59354445],\n",
       "       [0.74870291, 0.25435366],\n",
       "       [0.83762383, 1.24513099],\n",
       "       [0.33683985, 1.61422735],\n",
       "       [1.2286297 , 0.99539057],\n",
       "       [1.91930999, 0.63306178],\n",
       "       [1.99034747, 1.03912452],\n",
       "       [1.24847768, 0.69604729],\n",
       "       [1.37773738, 1.06521596],\n",
       "       [0.8004693 , 1.55599195],\n",
       "       [0.48809645, 0.34324182],\n",
       "       [1.01366421, 1.93280921],\n",
       "       [0.94682919, 0.04644054],\n",
       "       [0.23082683, 0.88874783]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.250160Z",
     "start_time": "2024-12-30T02:56:46.243928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = 4 + 3 * X1 + 5 * X2 + np.random.randn(100, 1)\n",
    "y"
   ],
   "id": "11d9425824c4a329",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19.18336858],\n",
       "       [11.204022  ],\n",
       "       [15.11201027],\n",
       "       [ 9.42219427],\n",
       "       [10.19703461],\n",
       "       [15.18338659],\n",
       "       [13.28223361],\n",
       "       [ 5.94100144],\n",
       "       [13.74102559],\n",
       "       [15.16913603],\n",
       "       [16.5212968 ],\n",
       "       [11.27594087],\n",
       "       [14.0656794 ],\n",
       "       [12.34483975],\n",
       "       [15.43142352],\n",
       "       [18.13709251],\n",
       "       [12.36898459],\n",
       "       [11.50081564],\n",
       "       [17.16428858],\n",
       "       [ 5.17993397],\n",
       "       [15.98500534],\n",
       "       [17.39046577],\n",
       "       [ 5.79078609],\n",
       "       [16.08904361],\n",
       "       [ 7.52882398],\n",
       "       [13.08894855],\n",
       "       [13.89887766],\n",
       "       [ 5.58797481],\n",
       "       [10.40979561],\n",
       "       [15.71627991],\n",
       "       [15.21261901],\n",
       "       [10.4079461 ],\n",
       "       [15.048406  ],\n",
       "       [ 6.17153783],\n",
       "       [11.33144331],\n",
       "       [11.31203816],\n",
       "       [13.3568095 ],\n",
       "       [ 9.20156155],\n",
       "       [14.40640485],\n",
       "       [13.89202814],\n",
       "       [13.99732873],\n",
       "       [ 7.44661011],\n",
       "       [13.03279381],\n",
       "       [12.91182242],\n",
       "       [13.72883756],\n",
       "       [14.33544901],\n",
       "       [15.69168557],\n",
       "       [12.45594005],\n",
       "       [ 7.32774299],\n",
       "       [13.87404144],\n",
       "       [13.54101634],\n",
       "       [ 9.97621168],\n",
       "       [10.75889362],\n",
       "       [11.19141089],\n",
       "       [14.55695427],\n",
       "       [15.36098116],\n",
       "       [ 8.28383733],\n",
       "       [11.77196028],\n",
       "       [15.58771426],\n",
       "       [10.8457787 ],\n",
       "       [12.69710302],\n",
       "       [12.05138295],\n",
       "       [14.10161193],\n",
       "       [ 8.61216621],\n",
       "       [ 9.03681391],\n",
       "       [15.54701391],\n",
       "       [12.87073842],\n",
       "       [ 9.34382727],\n",
       "       [16.17994218],\n",
       "       [12.06621739],\n",
       "       [16.46040633],\n",
       "       [13.27184834],\n",
       "       [11.14246068],\n",
       "       [ 7.65351993],\n",
       "       [ 9.4013969 ],\n",
       "       [12.73997656],\n",
       "       [13.75632454],\n",
       "       [14.23162334],\n",
       "       [13.5474282 ],\n",
       "       [11.075437  ],\n",
       "       [10.49860264],\n",
       "       [ 7.24929625],\n",
       "       [10.50885485],\n",
       "       [15.17691565],\n",
       "       [10.49485668],\n",
       "       [ 9.95508494],\n",
       "       [ 6.85455622],\n",
       "       [ 6.58494345],\n",
       "       [11.80110531],\n",
       "       [14.221168  ],\n",
       "       [14.56903681],\n",
       "       [13.60742046],\n",
       "       [15.63964972],\n",
       "       [10.92164864],\n",
       "       [13.62122401],\n",
       "       [15.25108464],\n",
       "       [ 9.65487157],\n",
       "       [17.10798453],\n",
       "       [ 6.55455317],\n",
       "       [ 9.4139981 ]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.257363Z",
     "start_time": "2024-12-30T02:56:46.251167Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# python 中驼峰原则创建一个类的对象，当然得加上小括号，\n",
    "# Python不用new关键字，然后把对象赋给一个变量\n",
    "lin_reg = LinearRegression()\n",
    "lin_reg"
   ],
   "id": "bbbc07405193eb0f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression()"
      ],
      "text/html": [
       "<style>#sk-container-id-1 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-1 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-1 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-1 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-1 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-1 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;&nbsp;LinearRegression<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LinearRegression.html\">?<span>Documentation for LinearRegression</span></a><span class=\"sk-estimator-doc-link \">i<span>Not fitted</span></span></label><div class=\"sk-toggleable__content \"><pre>LinearRegression()</pre></div> </div></div></div></div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.412125Z",
     "start_time": "2024-12-30T02:56:46.258369Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 接下来，我们去调用fit类的方法，这是非常sklearn的风格，然后去打印出结果\n",
    "# 截距项和参数系数\n",
    "lin_reg.fit(X, y)\n",
    "print(\"Intercept:\", lin_reg.intercept_)\n",
    "print(\"Coefficients:\", lin_reg.coef_)"
   ],
   "id": "b671f7fbab299bba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Intercept: [3.94895484]\n",
      "Coefficients: [[2.88463042 5.29039273]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.416144Z",
     "start_time": "2024-12-30T02:56:46.413125Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "b92fc18cee7b6895",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T02:56:46.423418Z",
     "start_time": "2024-12-30T02:56:46.416649Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 如果我们现在把这个参数调一下 fit_intercept=False\n",
    "lin_reg = LinearRegression(fit_intercept=False)\n",
    "lin_reg.fit(X, y)\n",
    "\n",
    "print(\"Intercept:\", lin_reg.intercept_)\n",
    "print(\"Coefficients:\", lin_reg.coef_)"
   ],
   "id": "36ff41a469198627",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Intercept: 0.0\n",
      "Coefficients: [[4.54011407 7.09300219]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T03:24:31.123652Z",
     "start_time": "2024-12-30T03:24:31.120043Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 模型预测\n",
    "X_new = np.array([[0],\n",
    "                  [2],\n",
    "                  [2]])\n",
    "# print(lin_reg.predict(X_new))\n",
    "\n",
    "# ValueError: X has 1 features, but LinearRegression is expecting 2 features as input.\n",
    "# ValueError: X有1个特征，但LinearRegression期望有2个特征作为输入。"
   ],
   "id": "776a532800922a07",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T03:10:53.625200Z",
     "start_time": "2024-12-30T03:10:53.618280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 模型预测\n",
    "# 记住，你怎么训练模型的，模型就具备什么样的功能！\n",
    "# 这里我们训练模型的时候用的训练集的X是两个特征，\n",
    "# 那么我们这里去使用模型的时候传给模型的X_new也得是两个维度\n",
    "\n",
    "X_new = np.array([[0, 0],\n",
    "                  [2, 1],\n",
    "                  [2, 4]])\n",
    "\n",
    "print(lin_reg.predict(X_new))"
   ],
   "id": "6d21f274716aa841",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.        ]\n",
      " [16.17323032]\n",
      " [37.45223688]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "991a06a43d4cfcd8"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
